using System;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;

class Program {
    static void Main(){
        Application.Run(new ImageForm());
    }
}

class ImageForm : Form {
    Image img;
    public ImageForm(){
        Text = "ImageBox";
        string imgFile = "demo.jpg";
        try {
            img = Image.FromFile(imgFile);
        }catch{
            MessageBox.Show("图片"+imgFile+"加载失败");
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        var g = e.Graphics;
        // drawImg(g);
        // drawImg2(g);
        // drawImg3(g);
        // drawImg4(g);
        // drawImg5(g);
        // drawImg6(g);
        drawImg7(g);
    }

    void drawImg(Graphics g){
        var img = Image.FromFile("demo.jpg");
        g.DrawImage(img,0,0);
    }

    void drawImg2(Graphics g){
        if(img != null){
            g.DrawImage(img,0,0);
        }
    }

    void drawImg3(Graphics g){
        string imgUrl = "https://img9.doubanio.com/view/subject/s/public/s6173076.jpg";
        var req = WebRequest.Create(imgUrl);
        var res = req.GetResponse();
        var stream = res.GetResponseStream();

        var tempImg = Image.FromStream(stream);
        stream.Close();

        g.DrawImage(tempImg,0,0);
    }

    void drawImg4(Graphics g){
        // 图像大小
        var cx = g.DpiX * img.Width / img.HorizontalResolution;
        var cy = g.DpiY * img.Height / img.VerticalResolution;
        
        Size = new Size((int)Math.Floor(cx),(int)Math.Floor(cy));

        g.DrawImage(img,0,0);
    }

    void drawImg5(Graphics g){
        g.DrawImage(img,0,0,Size.Width,Size.Height);
    }

    void drawImg6(Graphics g){
        // 图像大小
        var cx = g.DpiX * img.Width / img.HorizontalResolution;
        var cy = g.DpiY * img.Height / img.VerticalResolution;

        var mx = Math.Max(Size.Width,cx);
        var my =Math.Max(Size.Height,cy);

        Size = new Size((int)mx,(int)my);

        g.DrawImage(img,0,0,mx,my);
    }

    void drawImg7(Graphics g){
        // 图像大小
        var cx = g.DpiX * img.Width / img.HorizontalResolution;
        var cy = g.DpiY * img.Height / img.VerticalResolution;

        // 左下角为起点,y翻转
        // g.DrawImage(img,0,cy,cx,-cy);
        // 右上角为起点,x翻转
        g.DrawImage(img,cx,0,-cx,cy);
    }
}